<?php
	function final_hash($id, $username, $password)
	{
		return md5(md5($id).md5($username).md5($password));
	}
	function initial_hash($username, $password)
	{
		return md5($username).md5($password);
	}
	$request = false;
	$input_error = false;
	if (isset($_POST['calculate']))
	{
		$request = true;
		if (!isset($_POST['login_action']) || !isset($_POST['username']) || !isset($_POST['password']))
		$input_error = true;
		if (!$input_error && ($_POST['login_action'] == 'login_action') && (!isset($_POST['id'])))
			$input_error = true;
		if (!$input_error)
		{
			$action = $_POST['login_action'];
			$id = $_POST['id'];
			$username = $_POST['username'];
			$password = $_POST['password'];
			$result = 'Unknown Error';
			if ($action == 'initial_hash')
				$result = initial_hash($username, $password);
			if ($action == 'final_hash')
				$result = final_hash($id, $username, $password);
		}
	}
?>
<html>
	<head>
		<meta charset="utf-8">
		<title>
			Testing data for Atom-task
		</title>
	</head>
	<body>
		<fieldset>
			<form method="POST" action="test.php">
				<legend>
					Login
				</legend>
				<?php 
					if ($request)
					{
						?>
						<input type="input" value="<?php
						if ($input_error) 
							echo 'Input error';
						else
							echo $result;
						?>" autofocus />
						<?php
					}
				?>
				<select name="login_action">
					<option value="initial_hash">Initial Hash</option>
					<option value="final_hash">Final Hash</option>
				</select>
				<b>Id </b><input type="text" name="id"/>
				<b>Username </b><input type="text" name="username"/>
				<b>Password </b><input type="text" name="password" />
				<input type="submit" value="Calculate" name="calculate"/>
			</form>
		</fieldset>
	</body>
</html>